package com.fc.mall.portal.service.impl;

import com.fc.mall.base.object.Result;
import com.fc.mall.exception.ServiceException;
import com.fc.mall.mapper.OmsBargainMapper;
import com.fc.mall.mapper.OmsOrderGroupBuyMapper;
import com.fc.mall.mapper.PmsProductBargainMapper;
import com.fc.mall.portal.service.*;
import com.fc.mall.portal.vo.PmsProductVO;
import com.fc.mall.util.Utils;
import com.fc.mall.model.OmsBargain;
import com.fc.mall.model.OmsOrderGroupBuy;
import com.fc.mall.model.PmsProductBargain;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.imageio.ImageIO;
import javax.imageio.ImageReadParam;
import javax.imageio.ImageReader;
import javax.imageio.stream.ImageInputStream;
import java.awt.*;
import java.awt.geom.RoundRectangle2D;
import java.awt.image.BufferedImage;
import java.io.*;
import java.math.BigDecimal;
import java.net.URL;
import java.net.URLDecoder;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

@Service
public class PromoteServiceImpl implements PromoteService {

    @Autowired
    private UmsMemberService umsMemberService;
    @Autowired
    private ProductService productService;
    @Autowired
    private SysPropertyConfigService sysPropertyConfigService;

    @Autowired
    private ConfigService configService;

    @Autowired
    private OmsOrderGroupBuyMapper orderGroupBuyMapper;

    @Autowired
    private OmsBargainMapper bargainMapper;

    @Autowired
    private PmsProductBargainMapper pmsProductBargainMapper;


    /**
     *
     * @param productId   商品ID
     * @param orderGroupId  团订单ID 可以为null。。
     * @return
     */
    @Override
    public Result generateProductPromoteImage(Long productId, Long orderGroupId) {
        if(productId == null)
        {
            throw new ServiceException("产品Id不能为空!");
        }

        PmsProductVO productVO = productService.getInfo(productId);
        if(productVO == null)
        {
            throw new ServiceException("未找到选中商品!");
        }
        if(null == orderGroupId)
        {
            throw new ServiceException("团购订单不存在");
        }
        BigDecimal price = productVO.getPrice();
        Long prodGroupId = 0L;
        if(orderGroupId.intValue() != 0)
        {
            OmsOrderGroupBuy orderGroupBuy = orderGroupBuyMapper.selectByPrimaryKey(orderGroupId);
            if(null == orderGroupBuy)
            {
                throw new ServiceException("团购商品*1不存在");
            }
            price = orderGroupBuy.getGroupPrice();
            prodGroupId = orderGroupBuy.getProductGroupId();
        }
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        try {
            // 1. 获取背景图片的BufferedImage
            String backgroundImageUrl = sysPropertyConfigService.getPromoteShareImageBackgroundUrl();
            if(StringUtils.isEmpty(backgroundImageUrl))
            {
                throw new ServiceException("分享底图未设置");
            }
            BufferedImage backgroundBufferedImage = ImageIO.read(new URL(backgroundImageUrl));
            if(backgroundBufferedImage == null)
            {
                throw new ServiceException("未能成功读取分享底图!");
            }

            // 按照底图的宽高生成新的图片
            //底图的宽度
            int width = backgroundBufferedImage.getWidth(null);
            //底图的高度
            int height = backgroundBufferedImage.getHeight(null);
            BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
            Graphics2D g = image.createGraphics();
            g.drawImage(backgroundBufferedImage, 0, 0, width, height, null);
            // 添加Mall 标题
            String mallTitle = sysPropertyConfigService.getPromoteShareTitle();
            ProductPosterPicture.drewMallTitle(g, width, mallTitle);
            // 合并商品图片
            ProductPosterPicture.mergeProductImage(g, productVO.getPic());
            // 添加商品名称
            ProductPosterPicture.drewMallProductName(g, productVO.getName());
            // 添加商品价格
            ProductPosterPicture.drewProductPrice(g, price);
            //String page = "pages/index/index";
            mergeQrCodeImage(g, productId, orderGroupId, prodGroupId);
            g.dispose();
            ImageIO.write(image, "jpg", byteArrayOutputStream);
        }
        catch (Exception e)
        {
            throw new ServiceException(e.getMessage());
        }
        return Result.success(byteArrayOutputStream.toByteArray());
    }

    @Override
    public Result generatePersonalPoster() {
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        try {
            //获取背景图片
            // 1. 获取背景图片的BufferedImage
            String backgroundImageUrl = configService.getOptionalConfig("invite_image_background", "https://dobuymall.oss-cn-shanghai.aliyuncs.com/poster_bg.png");
            BufferedImage backgroundBufferedImage = ImageIO.read(new URL(backgroundImageUrl));
            if(backgroundBufferedImage == null)
            {
                throw new ServiceException("未能成功读取分享底图!");
            }
            // 按照底图的宽高生成新的图片
            // 底图的宽度
            int width = backgroundBufferedImage.getWidth(null);
            // 底图的高度
            int height = backgroundBufferedImage.getHeight(null);
            BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
            Graphics2D g = image.createGraphics();
            g.drawImage(backgroundBufferedImage, 0, 0, width, height, null);

            // 整合商家Logo
            String merchantLogo =  configService.getOptionalConfig("shopLogo", null);
            PersonalPosterPicture.mergeMerchantLogo(g, merchantLogo);
            // 整合个人邀请二维码
            String page = "pages/index/index";
            byte[] memberQrCode= umsMemberService.generateQrCode(page, new HashMap<>());
            PersonalPosterPicture.mergeQrCodeImage(g, memberQrCode);

            // 整合用户头像
            PersonalPosterPicture.mergeMemberHeadportal(g, umsMemberService.getCurrentMember().getIcon());
            String merchantName =  configService.getOptionalConfig("shopName", null);
            String nickname = umsMemberService.getCurrentMember().getNickname();
            PersonalPosterPicture.mergeAdviceTxt(g, width, nickname, merchantName);

            // ImageIO.write(image, "png", new File("E:/img/result.png"));
            ImageIO.write(image, "png", byteArrayOutputStream);
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        return Result.success(byteArrayOutputStream.toByteArray());
    }

    @Override
    public Result generateBargainPoster(Long bargainId) {
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        String backgroundImageUrl = "https://dobuymall.oss-cn-shanghai.aliyuncs.com/bargain_bg.png";
        try
        {
            BufferedImage backgroundBufferedImage = ImageIO.read(new URL(backgroundImageUrl));
            if(backgroundBufferedImage == null)
            {
                throw new ServiceException("未能成功读取分享底图!");
            }

            // 按照底图的宽高生成新的图片
            //底图的宽度
            int width = backgroundBufferedImage.getWidth(null);
            //底图的高度
            int height = backgroundBufferedImage.getHeight(null);
            BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
            Graphics2D g = image.createGraphics();
            g.drawImage(backgroundBufferedImage, 0, 0, width, height, null);

            OmsBargain bargain = bargainMapper.selectByPrimaryKey(bargainId);
            if(null == bargain)
            {
                throw new ServiceException("当前砍价不存在");
            }
            PmsProductBargain productBargain = pmsProductBargainMapper.selectByPrimaryKey(bargain.getProductBargainId());

            // merge 会员头像到海报
            BargainPosterPicture.mergeMemberHeadportal(g, bargain.getIcon());
            BargainPosterPicture.mergeBargainAdviceText(g, bargain.getNickname(), bargain.getProductName(), bargain.getOriginalMoney(), productBargain.getActivityPrice());
            BargainPosterPicture.mergeProduct(g, bargain.getPic());

            // 整合个人邀请二维码
            String page = "pages/bargain/bargaindetail/index";
            Map<String, Object> params = new HashMap<>();
            params.put("memberId", bargain.getOrderId());
            params.put("bargainId", bargain.getId());
            byte[] memberQrCode= umsMemberService.generateQrCode(page, params);
            BargainPosterPicture.mergeQrCodeImage(g, memberQrCode);
            //ImageIO.write(image, "png", new File("E:/img/result.png"));
            ImageIO.write(image, "png", byteArrayOutputStream);
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        return Result.success(byteArrayOutputStream.toByteArray());
    }









    private static BufferedImage roundImage(BufferedImage image, int targetSize, int cornerRadius) {
        BufferedImage outputImage = new BufferedImage(targetSize, targetSize, BufferedImage.TYPE_INT_ARGB);
        Graphics2D g2 = outputImage.createGraphics();
        g2.setComposite(AlphaComposite.Src);
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g2.setColor(Color.WHITE);
        g2.fill(new RoundRectangle2D.Float(0, 0, targetSize, targetSize, cornerRadius, cornerRadius));
        g2.setComposite(AlphaComposite.SrcIn);
        g2.drawImage(image, 0, 0, null);
        g2.dispose();
        return outputImage;
    }




    /**
     * 合并会员推广二维码
     * @param g
     * @throws Exception
     */
    private void mergeQrCodeImage(Graphics2D g, Long productId, Long orderGroupId, Long groupId) throws Exception
    {
        String page;
        Map<String, Object> params = new HashMap<>();

        if(orderGroupId.intValue() == 0)
        {
            page = "pages/catalog/product/detail/index";
            params.put("prodId", productId);
        }
        else
        {
            page = "pages/grouppurchase/group/index";
            params.put("orderGroupId", orderGroupId);
            params.put("groupId", groupId);
        }

        // 1. 获取当前登录用户的二维码
        byte[] memberQrCode = umsMemberService.generateQrCode(page, params);
        InputStream inputStream = new ByteArrayInputStream(memberQrCode);
        BufferedImage qrCodeBufferImage = ImageIO.read(inputStream);
        g.drawImage(qrCodeBufferImage, 430, 700, 200, 200, null);
    }

    /**
     * 剪切照片为正方形
     * @param img
     * @return
     */
    private static  BufferedImage cutToSquarePicture(BufferedImage img) throws IOException {
        // 原图的宽度
        int width = img.getWidth();
        //原图的高度
        int height = img.getHeight();

        if(width == height)
        {
            return img;
        }
        // 要截图的坐标
        int newImage_x = 0;
        // 要截图的坐标
        int newImage_y = 0;
        // 截取的长
        int newWidth = width;
        // 截取的宽
        int newHeight = height;
        if(width > height)
        {
            newImage_x = (width - height) / 2;
            newWidth = height;
        }
        else
        {
            newImage_y = (height - width) / 2;
            newHeight = width;
        }
        return cutJPG(img, newImage_x, newImage_y, newWidth, newHeight);
    }

    /**
     * 进行裁剪操作
     *
     * @param originalImage the original image
     * @param x             the x
     * @param y             the y
     * @param width         the width
     * @param height        the height
     * @return the buffered image
     * @throws IOException the io exception
     */
    private static BufferedImage cutJPG(BufferedImage originalImage, int x, int y, int width, int height) throws IOException {
        Iterator<ImageReader> iterator = ImageIO.getImageReadersByFormatName("jpg");
        ImageReader reader = iterator.next();
        // 转换成字节流
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        ImageIO.write(originalImage, "jpg", outputStream);
        InputStream is = new ByteArrayInputStream(outputStream.toByteArray());

        ImageInputStream iis = ImageIO.createImageInputStream(is);
        reader.setInput(iis, true);
        ImageReadParam param = reader.getDefaultReadParam();
        Rectangle rect = new Rectangle(x, y, width, height);
        param.setSourceRegion(rect);
        BufferedImage bi = reader.read(0, param);
        return bi;
    }

    /**
     * 图片拉伸
     *
     * @param img the original image
     * @param originWidth   the origin width
     * @param originHeight  the origin height
     * @return the buffered image
     * @throws Exception the exception
     */
    private static BufferedImage zoomPicture(BufferedImage img, int originWidth, int originHeight) throws Exception {
        // 原图的宽度
        int width = img.getWidth();

        //原图的高度
        int height = img.getHeight();

        int scaledWidth = width;
        int scaledHeight = height;
        // 如果不是正方形
        if (width == height) {
            // 按照originHeight进行缩放
            scaledWidth = originHeight;
            scaledHeight = originHeight;
        } else {
            if (width > height) {
                // 按照originHeight进行缩放
                scaledWidth = (scaledWidth * originHeight) / scaledHeight;
                scaledHeight = originHeight;
            } else {
                // 宽高比例
                int originPercent = (originHeight * 100) / originWidth;
                int newPercent = (height * 100) / width;
                if (newPercent >= originPercent) {
                    // 按照originWidth进行缩放
                    scaledWidth = originWidth;
                    scaledHeight = (originHeight * scaledWidth) / scaledWidth;
                } else {
                    // 按照originHeight进行缩放
                    scaledWidth = (scaledWidth * originHeight) / scaledHeight;
                    scaledHeight = originHeight;
                }
            }
        }
        Image schedImage = img.getScaledInstance(scaledWidth, scaledHeight, Image.SCALE_SMOOTH);
        // 新的图片
        BufferedImage bufferedImage = new BufferedImage(scaledWidth, scaledHeight, img.getType() == 0 ? 5: img.getType());
        Graphics2D g = bufferedImage.createGraphics();
        // 绘制
        g.drawImage(schedImage, 0, 0, null);
        g.dispose();
        return bufferedImage;
    }



    private ByteArrayOutputStream generateProductPosterImage(PmsProductVO productVO)
    {
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        try {
            // 1. 获取背景图片的BufferedImage
            String backgroundImageUrl = sysPropertyConfigService.getPromoteShareImageBackgroundUrl();
            if(StringUtils.isEmpty(backgroundImageUrl))
            {
                throw new ServiceException("分享底图未设置");
            }
            BufferedImage backgroundBufferedImage = ImageIO.read(new URL(backgroundImageUrl));
            if(backgroundBufferedImage == null)
            {
                throw new ServiceException("未能成功读取分享底图!");
            }

            // 按照底图的宽高生成新的图片
            //底图的宽度
            int width = backgroundBufferedImage.getWidth(null);
            //底图的高度
            int height = backgroundBufferedImage.getHeight(null);
            BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
            Graphics2D g = image.createGraphics();
            g.drawImage(backgroundBufferedImage, 0, 0, width, height, null);

            // 添加Mall 标题
            String mallTitle = sysPropertyConfigService.getPromoteShareTitle();
            ProductPosterPicture.drewMallTitle(g, width, mallTitle);

            // 合并商品图片
            ProductPosterPicture.mergeProductImage(g, productVO.getPic());

            // 添加商品名称
            ProductPosterPicture.drewMallProductName(g, productVO.getName());

            // 添加商品价格
            ProductPosterPicture.drewProductPrice(g, productVO.getPrice());

            g.dispose();
            ImageIO.write(image, "jpg", byteArrayOutputStream);
        }
        catch (Exception e)
        {
            throw new ServiceException(e.getMessage());
        }
        return byteArrayOutputStream;
    }


    private ByteArrayOutputStream generateGroupBuyPosterImage(Long orderGroupId)
    {
        OmsOrderGroupBuy orderGroupBuy = orderGroupBuyMapper.selectByPrimaryKey(orderGroupId);

        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();

        try
        {
            String backgroundImageUrl = "https://dobuymall.oss-cn-shanghai.aliyuncs.com/group_bg.png";
            BufferedImage backgroundBufferedImage = ImageIO.read(new URL(backgroundImageUrl));
            if(backgroundBufferedImage == null)
            {
                throw new ServiceException("未能成功读取分享底图!");
            }

            // 按照底图的宽高生成新的图片
            // 底图的宽度
            int width = backgroundBufferedImage.getWidth(null);
            // 底图的高度
            int height = backgroundBufferedImage.getHeight(null);
            BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
            Graphics2D g = image.createGraphics();
            g.drawImage(backgroundBufferedImage, 0, 0, width, height, null);


        }
        catch (Exception e)
        {
            throw new ServiceException(e.getMessage());
        }

        return byteArrayOutputStream;
    }

    public static class BargainPosterPicture {

        private static void mergeMemberHeadportal(Graphics2D g, String memberHeadportal) throws Exception
        {
            if(StringUtils.isEmpty(memberHeadportal))
            {
                return;
            }
            // 1. 获取会员头像图片流
            BufferedImage memberHeadportalBufferedImage = ImageIO.read(new URL(memberHeadportal));
            // 2. 把商品的图片剪切成正方形
            BufferedImage squaredMerchantLogoImage = roundImage(memberHeadportalBufferedImage, 86, 86);
            g.drawImage(squaredMerchantLogoImage, 25, 55, 86, 86, null);
        }

        private static void mergeProduct(Graphics2D g, String prodPic) throws Exception
        {
            // 1. 获取产品介绍图片流
            BufferedImage productBufferedImage = ImageIO.read(new URL(prodPic));
            // 2. 把商品的图片剪切成正方形
            BufferedImage squaredMerchantLogoImage = cutToSquarePicture(productBufferedImage);
            // 3. 拉伸或缩放图片
            BufferedImage zoomProductImage = zoomPicture(squaredMerchantLogoImage, 162, 162);
            g.drawImage(zoomProductImage, 23, 187, 162, 162, null);
        }

        private static void mergeBargainAdviceText(Graphics2D g, String nickname, String productName, BigDecimal orginalPrice, BigDecimal activityPrice) throws  Exception
        {
            if(StringUtils.isEmpty(nickname))
            {
                return;
            }
            nickname = Utils.urlDecoderTransfer(nickname);

            // 普通字体
            Font font = new Font("黑体", Font.BOLD, 26);
            g.setFont(font);
            g.setColor(new Color(255, 255, 255));
            g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
            g.drawString(nickname, 123, 76);

            String msg = "小哥哥，小姐姐帮忙砍一刀";

            // 普通字体
            Font descFont = new Font("黑体", Font.PLAIN, 26);
            g.setFont(descFont);
            g.setColor(new Color(255, 255, 255));
            g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
            g.drawString(msg, 122, 127);

            // 产品名称
            productName = productName.substring(0, 10).concat("...");
            Font productNameFont = new Font("黑体", Font.PLAIN, 26);
            g.setFont(productNameFont);
            g.setColor(new Color(48, 48, 48));
            g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
            g.drawString(productName, 215, 214);


            String currentPrice = "当前价格: ￥" + orginalPrice.toString();
            Font prodPriceFont = new Font("黑体", Font.PLAIN, 20);
            g.setFont(prodPriceFont);
            g.setColor(new Color(155, 155, 155));
            g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
            g.drawString(currentPrice, 215, 254);


            String bargainPrice = "砍后价格: ￥" + activityPrice.toString();
            Font bargainPriceFont = new Font("黑体", Font.PLAIN, 20);
            g.setFont(bargainPriceFont);
            g.setColor(new Color(225, 50, 56));
            g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
            g.drawString(bargainPrice, 215, 300);

        }

        private static void mergeQrCodeImage(Graphics2D g,  byte[] memberQrCode) throws Exception
        {
            // 6. 获取当前登录用户的二维码
            InputStream inputStream = new ByteArrayInputStream(memberQrCode);
            BufferedImage qrCodeBufferImage = ImageIO.read(inputStream);
            g.drawImage(qrCodeBufferImage, 168, 396, 234, 234, null);
        }
    }


    public static class ProductPosterPicture {
        /**
         * 添加Mall 标题
         * @param g
         * @param backgroundWidth
         */
        private static void drewMallTitle(Graphics2D g, int backgroundWidth, String title)
        {
            // 普通字体
            Font font = new Font("黑体", Font.PLAIN, 26);
            g.setFont(font);
            g.setColor(new Color(68, 68, 68));
            g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
            FontMetrics fm = g.getFontMetrics(font);
            // 昵称长度和放置的位置
            int textWidth = fm.stringWidth(title);
            g.drawString(title, (backgroundWidth - textWidth) / 2, 40);
        }


        /**
         * 合并产品照片
         * @param g
         * @param productPic
         * @throws Exception
         */
        private static void mergeProductImage(Graphics2D g, String productPic) throws Exception
        {
            // 1. 获取产品介绍图片流
            BufferedImage productBufferedImage = ImageIO.read(new URL(productPic));
            // 2. 把商品的图片剪切成正方形
            BufferedImage squaredProductImage = cutToSquarePicture(productBufferedImage);
            // 3. 拉伸或缩放图片
            BufferedImage zoomProductImage = zoomPicture(squaredProductImage, 600, 600);

            g.drawImage(zoomProductImage, 30, 60, 600, 600, null);
        }


        /**
         * 添加Mall 标题
         * @param g
         * @param productName
         */
        private static void drewMallProductName(Graphics2D g, String productName)
        {
            // 普通字体
            Font font = new Font("黑体", Font.PLAIN, 26);
            g.setFont(font);
            FontMetrics fm = g.getFontMetrics(font);
            int textWidth = fm.stringWidth(productName);
            g.setColor(new Color(85, 85, 85));
            g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
            // 先按字节来换行，英文单词空格问题暂时未考虑
            int prodNameLength = productName.length();
            int lineLength = 14;
            int lineHeight = 35;
            if (prodNameLength > lineLength) {
                int roop =  prodNameLength / lineLength;
                for(int i = 0; i< roop ; i++)
                {
                    String drawString = productName.substring(lineLength*i, lineLength*(i+1));
                    g.drawString(drawString, 30, 720 + i * lineHeight);
                }
                if(prodNameLength > roop * lineLength)
                {
                    String drawString = productName.substring(roop * lineLength);
                    g.drawString(drawString, 30, 720 + roop * lineHeight);
                }
            }
            else
            {
                g.drawString(productName, 30, 720);
            }
        }

        /**
         * 添加商品价格
         * @param g
         * @param price
         */
        private static void drewProductPrice(Graphics2D g, BigDecimal price)
        {
            // 普通字体
            Font font = new Font("黑体", Font.PLAIN, 26);
            g.setFont(font);
            g.setColor(new Color(255, 0, 0));
            g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
            FontMetrics fm = g.getFontMetrics(font);
            String priceStr = price.setScale(2).toString();
            // 昵称长度和放置的位置
            g.drawString(priceStr, 30, 910);
        }
    }



    public static class PersonalPosterPicture {
        /**
         * 整合商家Logo
         * @param g
         */
        private static void mergeMerchantLogo(Graphics2D g, String merchantLogo) throws Exception
        {

            // 1. 获取产品介绍图片流
            BufferedImage merchantLogoBufferedImage = ImageIO.read(new URL(merchantLogo));
            // 2. 把商品的图片剪切成正方形
            BufferedImage squaredMerchantLogoImage = cutToSquarePicture(merchantLogoBufferedImage);
            // 3. 拉伸或缩放图片
            BufferedImage zoomProductImage = zoomPicture(squaredMerchantLogoImage, 70, 70);

            g.drawImage(zoomProductImage, 202, 216, 70, 70, null);
        }


        private static void mergeQrCodeImage(Graphics2D g,  byte[] memberQrCode) throws Exception
        {
            // 6. 获取当前登录用户的二维码
            InputStream inputStream = new ByteArrayInputStream(memberQrCode);
            BufferedImage qrCodeBufferImage = ImageIO.read(inputStream);
            g.drawImage(qrCodeBufferImage, 188, 348, 286, 286, null);
        }

        private static void mergeMemberHeadportal(Graphics2D g, String memberHeadportal) throws Exception
        {
            if(StringUtils.isEmpty(memberHeadportal))
            {
                return;
            }
            // 1. 获取会员头像图片流
            BufferedImage memberHeadportalBufferedImage = ImageIO.read(new URL(memberHeadportal));
            // 2. 把商品的图片剪切成正方形
            BufferedImage squaredMerchantLogoImage = roundImage(memberHeadportalBufferedImage, 100, 100);

            g.drawImage(squaredMerchantLogoImage, 64, 666, 100, 100, null);
        }


        /**
         *
         */
        private static void mergeAdviceTxt(Graphics2D g,  int backgroundWidth, String nickname, String merchantName) throws  Exception
        {

            if(StringUtils.isEmpty(nickname))
            {
                return;
            }
            nickname = "我是" + URLDecoder.decode(nickname, "utf-8");

            // 普通字体
            Font font = new Font("黑体", Font.BOLD, 30);
            g.setFont(font);
            g.setColor(new Color(68, 68, 68));
            g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
            FontMetrics fm = g.getFontMetrics(font);
            // 昵称长度和放置的位置
            int textWidth = fm.stringWidth(nickname);
            int x = (backgroundWidth - textWidth) / 2;
            g.drawString(nickname, x, 754);



            if(StringUtils.isEmpty(merchantName))
            {
                return;
            }
            merchantName = "我为" + merchantName + "代言";

            // 普通字体
            Font descFont = new Font("黑体", Font.PLAIN, 20);
            g.setFont(descFont);
            g.setColor(new Color(68, 68, 68));
            g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
            FontMetrics descfm = g.getFontMetrics(descFont);
            // 昵称长度和放置的位置
            int descFontWidth = descfm.stringWidth(merchantName);
            int descX = (backgroundWidth - descFontWidth) / 2;
            g.drawString(merchantName, descX, 796);
        }
    }

}
